﻿#include <stdio.h>
#include <math.h>
//7-1 计算阶乘和
void ex3_7_1_1()
{
	int n = 0;
	int sum = 0;
	int item = 1;

	scanf("%d", &n);

	for (int i = 1; i <= n; i++) {
		item = 1;
		for (int j = 1; j <= i; j++) {
			item *= j;
		}
		sum += item;
	}

	printf("%d\n", sum);
}

// 7-1 计算阶乘和
void ex3_7_1_2()
{
	int n = 0;
	int sum = 0;
	int item = 1;

	scanf("%d", &n);

	for (int i = 1; i <= n; i++) {
		item *= i;
		sum += item;
	}

	printf("%d\n", sum);
}

//7-2 乘法口诀表
void ex3_7_2()
{
	int n;

	scanf("%d", &n);

	for (int row = 1; row <= n; row++) {
		for (int col = 1; col <= row; col++) {
			printf("%d*%d=%d ", col, row, row * col);
		}
		printf("\n");
	}
}

//7-3 计算圆周率 for
void ex3_7_3_1()
{
	double eps = 0.0;
	double item = 1.0;
	double sum = 1.0;

	scanf("%lf", &eps);

	for (int i = 1; item >= eps; i++) {
		item = item * i / (2.0 * i + 1);
		sum += item;
	}

	printf("%.6f", sum * 2);
}
//7-3 计算圆周率 while
void ex3_7_3_2()
{
	double eps = 0.0;
	double item = 1.0;
	double sum = 1.0;
	int i = 1;

	scanf("%lf", &eps);

	do {
		item = item * i / (2.0 * i + 1);
		i++;
		sum += item;
	} while (item > eps);

	printf("%.6f", sum * 2);
}

//7-4 斐波那契数列
void ex3_7_4()
{
	int n;
	int f1 = 1;
	int f2 = 2;
	int month = 3;

	scanf("%d", &n);

	if (n == 1) {
		month = 1;
	}

	while (f2 < n && month++) {
		f2 = f1 + f2;
		f1 = f2 - f1;
	}

	printf("%d", month);
}

//7-5 猴子吃桃问题
void ex3_7_5()
{
	int n = 0;
	int sum = 1;

	scanf("%d", &n);

	for (int i = 1; i < n; ++i) {
		sum = (1 + sum) * 2;
	}

	printf("%d", sum);
}

//7-6 单词首字母大写
void ex3_7_6()
{
	char ch;
	int first = 1;

	while ((ch = getchar()) != '\n') {
		if (ch == ' ') {
			putchar(ch);
			first = 1;
			continue;
		}

		if (first && ch >= 'a' && ch <= 'z') {
			putchar(ch - 32);
		}
		else {
			putchar(ch);
		}

		first = 0;
	}
}
//7-7 输出整数各位数字
void ex3_7_7_1()
{
	char ch;
	while ((ch = getchar()) != '\n') {
		printf("%c ", ch);
	}
}

//7-7 输出整数各位数字
void ex3_7_7_2()
{
	int n, d;
	int pow = 1;

	scanf("%d", &n);
	d = n;

	while (d > 9) {
		d = d / 10;
		pow = pow * 10;
	}

	do {
		printf("%d ", n / pow);
		n %= pow;
	} while (pow /= 10);
}

//7-8 打印沙漏
//2*n^2 - 1 <= N
void ex3_7_8()
{
	int N = 0;
	int n = 0;
	char ch;
	scanf("%d %c", &N, &ch);

	n = sqrt((N + 1) / 2.0);
	//top half
	for (int i = n; i >= 1; i--) {
		for (int j = 0; j < n - i; j++) {
			printf(" ");
		}
		for (int j = 0; j < 2 * i - 1; j++) {
			printf("%c", ch);
		}
		printf("\n");
	}
	//bottom half
	for (int i = 2; i <= n; i++) {
		for (int j = 0; j < n - i; j++) {
			printf(" ");
		}
		for (int j = 0; j < 2 * i - 1; j++) {
			printf("%c", ch);
		}
		printf("\n");
	}

	printf("%d\n", N - 2 * n * n + 1);
}

//7-9 龟兔赛跑 O(1)
void ex3_7_9_1()
{
	int minutes = 0;
	int distance = 0;

	scanf("%d", &minutes);

	distance = 270 * (minutes / 90);
	minutes %= 90;

	if (minutes == 0 || minutes == 30 || minutes == 45 || minutes == 60) {
		printf("-_- %d", distance + minutes * 3);
		return;
	}
	if (minutes < 10) {
		printf("^_^ %d", distance + minutes * 9);
	}
	else if (minutes < 30) {
		printf("^_^ %d", distance + 90);
	}
	else if (minutes < 45) {
		printf("@_@ %d", distance + 90 + (minutes - 30) * 3);
	}
	else if (minutes < 60) {
		printf("^_^ %d", distance + 180);
	}
	else {
		printf("@_@ %d", distance + 180 + (minutes - 60) * 3);
	}
}
//7-9 龟兔赛跑 简单模拟O(n)
void ex3_7_9_2()
{
	int minutes = 0;
	int turtle = 0;
	int rabbit = 0;
	int not_sleep = 1;
	int sleep_time = 1;

	scanf("%d", &minutes);

	for (int i = 1; i <= minutes; i++) {
		turtle += 3;
		if (not_sleep || sleep_time++ > 30) {
			rabbit += 9;
			if (rabbit > turtle && i % 10 == 0) {
				not_sleep = 0;
				sleep_time = 1;
			}
		}
	}

	if (rabbit > turtle)
		printf("^_^ %d\n", rabbit);
	else if (rabbit < turtle)
		printf("@_@ %d\n", turtle);
	else
		printf("-_- %d\n", turtle);
}

int main()
{
	return 0;
}